package com.panfeng.xcloud.scheduler.util.tron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.panfeng.xcloud.common.core.enums.ResponseStatusEnum;
import com.panfeng.xcloud.common.core.exceptions.BaseBizException;
import org.springframework.web.client.RestTemplate;
import org.tron.protos.contract.Common;

import java.math.BigDecimal;
import java.util.LinkedHashMap;

/**
 * @author: sunlight
 * @date: 2020/9/22 14:11
 */
public class TrxHelper {

    public static TransactionResult transfer(String senderPrivateKey, String receiveAddress, BigDecimal value) throws Exception {
        String result = TransactionSender.sendTransaction(
                senderPrivateKey,
                new TrxTransferBuilder(
                        AddressHelper.privateKeyToBase58Address(senderPrivateKey),
                        receiveAddress,
                        value)
        );
        return TransactionResult.parse(result);
    }

    public static TransactionResult freezeBalance(String senderPrivateKey, BigDecimal frozenBalance, String receiverAddress, Common.ResourceCode resourceCode) throws Exception {
        String result = TransactionSender.sendTransaction(
                senderPrivateKey,
                new TrxFreezeBalanceBuilder(
                        frozenBalance,
                        receiverAddress,
                        resourceCode,
                        AddressHelper.privateKeyToBase58Address(senderPrivateKey)
                )
        );
        return TransactionResult.parse(result);
    }

    public static TransactionResult freezeBalance(String senderPrivateKey, BigDecimal frozenBalance, Common.ResourceCode resourceCode) throws Exception {
        String result = TransactionSender.sendTransaction(
                senderPrivateKey,
                new TrxFreezeBalanceBuilder(
                        frozenBalance,
                        resourceCode,
                        AddressHelper.privateKeyToBase58Address(senderPrivateKey)
                )
        );
        return TransactionResult.parse(result);
    }

    public static TransactionResult unFreezeBalance(String senderPrivateKey, Common.ResourceCode resourceCode) throws Exception {
        String result = TransactionSender.sendTransaction(
                senderPrivateKey,
                new TrxUnFreezeBalanceBuilder(
                        resourceCode,
                        AddressHelper.privateKeyToBase58Address(senderPrivateKey)
                )
        );
        return TransactionResult.parse(result);
    }

    public static TransactionResult unFreezeBalance(String senderPrivateKey, Common.ResourceCode resourceCode, String receiverAddress) throws Exception {
        String result = TransactionSender.sendTransaction(
                senderPrivateKey,
                new TrxUnFreezeBalanceBuilder(
                        receiverAddress,
                        resourceCode,
                        AddressHelper.privateKeyToBase58Address(senderPrivateKey)
                )
        );
        return TransactionResult.parse(result);
    }

    public static BigDecimal getTrxBalance(String address, RestTemplate restTemplate) {
        String url = "https://api.trongrid.io/v1/accounts/" + address;
        JSONObject reqData = restTemplate.getForObject(url, JSONObject.class);
        if (reqData.getBoolean("success")) {
            JSONArray array = reqData.getJSONArray("data");
            if (array.size() > 0) {
                JSONObject data = (JSONObject) (array.get(0));
                BigDecimal balance = data.getBigDecimal("balance");
                return balance;
            }
            throw new BaseBizException(ResponseStatusEnum.FAIL);
        } else {
            throw new BaseBizException(ResponseStatusEnum.FAIL);
        }
    }

    public static void transferTrxNew(String senderPrivateKey,String toAddress,int amount, RestTemplate restTemplate) {
        String url = "https://api.trongrid.io/wallet/easytransferbyprivate";

        JSONObject param = new JSONObject(new LinkedHashMap());
        param.put("privateKey",senderPrivateKey);
        param.put("toAddress",toAddress);
        param.put("amount",amount);

        JSONObject resp = restTemplate.postForObject(url,param,JSONObject.class);
        System.out.println(resp);
        /*if (reqData.getBoolean("success")) {
            JSONArray array = reqData.getJSONArray("data");
            if (array.size() > 0) {
                JSONObject data = (JSONObject) (array.get(0));
                BigDecimal balance = data.getBigDecimal("balance");
                return balance;
            }
            throw new BaseBizException(ResponseStatusEnum.FAIL);
        } else {
            throw new BaseBizException(ResponseStatusEnum.FAIL);
        }*/
    }

}
